package com.cn.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfig {
    /**
     * 全局 过滤器
     *
     * @return
     */
    @Bean
    public CorsWebFilter corsWebFilter() {

        CorsConfiguration config = new CorsConfiguration();
//        设置允许的头信息
        config.addAllowedHeader("*");
//        设置允许的请求方式
        config.addAllowedMethod("*");
//        设置允许的域
        config.addAllowedOrigin("*");
//        允许访问cookie
        config.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource urlSource = new UrlBasedCorsConfigurationSource();
//        拦截路径, 拦截规则  registerCorsConfiguration("拦截路径", 拦截规则);
        urlSource.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(urlSource);
    }
}

